import {
  ClassSerializerInterceptor,
  Controller,
  Post,
  UseInterceptors,
  UseGuards,
  Request,
  Body,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { UserEntity } from 'src/user/entities/user.entity';
import { SkipAuth } from './decorator/skip-auth.decorator';
import { IpAddress } from './decorator/ip-address.decorator';
import { CreateUserDto } from 'src/user/dto/create-user.dto';
import { UserService } from 'src/user/user.service';
import { BusinessException } from 'src/common/filter/business-exception/business.exception';

@ApiTags('授权')
@Controller('auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly userService: UserService,
  ) {}

  @ApiOperation({ summary: '登录授权' })
  @UseGuards(AuthGuard('local'))
  @Post('login')
  @SkipAuth()
  @UseInterceptors(ClassSerializerInterceptor)
  login(@Request() req: { user: UserEntity }, @IpAddress() ip: string) {
    return this.authService.login(req.user, ip);
  }
  @ApiOperation({ summary: '注册' })
  @Post('sign')
  @SkipAuth()
  @UseInterceptors(ClassSerializerInterceptor)
  sign(@Body() createUserDto: CreateUserDto) {
    throw new BusinessException('暂不支持注册，请联系管理员');
    return this.userService.create(createUserDto);
  }
}
